﻿@using Nop.Admin.Models.Logistics
@using Telerik.Web.Mvc
@model LogisticsCourierCommissionModel
@using Telerik.Web.Mvc.UI

@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    //page title
    ViewBag.Title = @T("Admin.Logistics.Commission.CommissionPage").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/subsites/Administration/Content/images/ico-catalog.png")" />
            @T("Admin.Logistics.Commission.CommissionPage")  ---   @T("Admin.Logistics.District").Text: @Model.DistrictName
        </div>
        @*
            <div class="options">
                <a href="@Url.Action("Create")" class="t-button">@T("Admin.Common.AddNew")</a>
                <a href="@Url.Action("ExportXml")" class="t-button">@T("Admin.Common.ExportToXml")</a>
            </div>
        *@
    </div>
    @Html.HiddenFor(model => model.DistrictId);

    <table width="100%" class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.StartDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.EndDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.EndDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CourierId):
            </td>
            <td class="adminData">
                @(Html.Telerik().DropDownList()
                        .Name("CourierId")
                        .DataBinding(binding => binding.Ajax().Select("CourierList", "Logistics", new { districtId = Model.DistrictId }))
                        .Placeholder(T("Admin.Logistics.SelectCourier").Text)
                        .DropDownHtmlAttributes(
                            new { style = "width: 400px;", })
                )
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" id="search-courier" class="t-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p></p>
    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<PlannedRouteModel>()
                        .Name("courier-plan-grid-admin")
                        .ClientEvents(events => events
                            .OnDataBinding("onDataBinding")
                            .OnDataBound("onDataBound")
                            .OnComplete("onComplete"))
                        .Columns(columns =>
                        {
                            columns.Bound(x => x.PlanId)
                                .Centered();
                            columns.Bound(x => x)
                                .ClientTemplate("<span><#= CustomProperties[\"SumFailPickUp\"] #></span>")
                                .Centered()
                                .Title(T("Admin.Logistics.Commission.FailPickUp").Text);
                            columns.Bound(x => x)
                                .ClientTemplate("<span><#= CustomProperties[\"SumFailDelivery\"] #></span>")
                                .Centered()
                                .Title(T("Admin.Logistics.Commission.FailDelivery").Text);
                            columns.Bound(x => x)
                                .ClientTemplate("<span><#= CustomProperties[\"SumSuccessDelivery\"] #></span>")
                                .Centered()
                                .Title(T("Admin.Logistics.Commission.SuccessDelivery").Text);
                            columns.Bound(x => x)
                                .ClientTemplate("<span><#= CustomProperties[\"SumSuccessPickUp\"] #></span>")
                                .Centered()
                                .Title(T("Admin.Logistics.Commission.SuccessPickUp").Text)
                                .FooterTemplate(
                                 string.Format("<div><strong>{0}:</strong></div>" +
                                 "<div>{1}: <span id=\"aggregator-fail-delivery-block\"></span></div>" +
                                 "<div>{2}: <span id=\"aggregator-fail-pickup-block\"></span></div>" +
                                 "<div>{3}: <span id=\"aggregator-success-delivery-block\"></span></div>" +
                                 "<div>{4}: <span id=\"aggregator-success-pickup-block\"></span></div>",
                                 @T("Admin.Orders.Report.Summary").Text,
                                 @T("Admin.Logistics.Commission.TotalFailDelivery").Text,
                                 @T("Admin.Logistics.Commission.TotalFailPickUp").Text,
                                 @T("Admin.Logistics.Commission.TotalSuccessDelivery").Text,
                                 @T("Admin.Logistics.Commission.TotalSuccessPickUp").Text));
                        })
                        .DetailView(detailView => detailView.ClientTemplate(Html.Telerik().Grid<PackageLocationModel>()
                                .Name("package-location-grid-<#= PlanId #>")
                                .Columns(columns =>
                                {
                                    columns.Bound(pl => pl.PackageId)
                                        .Centered()
                                        .Template(pl => Html.ActionLink(pl.PackageId.ToString(), "PackageEdit", new { id = pl.PackageId }))
                                        .ClientTemplate("<a href=\"PackageEdit/<#= PackageId #>\"><#= PackageId #></a>")
                                        .Width(35)
                                        .Title(T("Admin.Logistics.Common.Fields.PackageId").Text);
                                    columns.Bound(x => x.Address.FormattedAddress)
                                        .Title(T("Admin.Logistics.Common.Fields.Address").Text);
                                    columns.Bound(x => x.Action)
                                        .Title(T("Admin.Logistics.PlannedRoute.Fields.Action").Text);
                                    columns.Bound(x => x.ShippingStatusStr)
                                        .Title(T("Admin.Logistics.Common.Fields.ShippingStatus").Text);
                                    columns.Bound(x => x.UpdatedOn)
                                        .Template(x => x.UpdatedOnStr)
                                        .ClientTemplate("<#= UpdatedOnStr #>");
                                })
                                .DataBinding(dataBinding => dataBinding.Ajax()
                                            .Select("GetPackageLocations", "Logistics", new
                                            {
                                                planId = "<#= PlanId #>"
                                            }))
                                            .ToHtmlString()
                               )
                               .Template(@<text>
                                @(Html.Telerik().Grid(item.PackageLocations)
                                    .Name("package-location-grid-" + item.PlanId)
                                    .Columns(columns =>
                                    {
                                        columns.Bound(pl => pl.PackageId)
                                        .Centered()
                                        .Template(pl => Html.ActionLink(pl.PackageId.ToString(), "PackageEdit", new { id = pl.PackageId }))
                                        .ClientTemplate("<a href=\"PackageEdit/<#= PackageId #>\"><#= PackageId #></a>")
                                        .Width(35)
                                        .Title(T("Admin.Logistics.Common.Fields.PackageId").Text);
                                        columns.Bound(x => x.Address.FormattedAddress)
                                            .Title(T("Admin.Logistics.Common.Fields.Address").Text);
                                        columns.Bound(x => x.Action)
                                            .Title(T("Admin.Logistics.PlannedRoute.Fields.Action").Text);
                                        columns.Bound(x => x.ShippingStatusStr)
                                            .Title(T("Admin.Logistics.Common.Fields.ShippingStatus").Text);
                                        columns.Bound(x => x.UpdatedOn)
                                            .Template(x => x.UpdatedOnStr)
                                            .ClientTemplate("<#= UpdatedOnStr #>");
                                    })
                                )
                            </text>))
                    .DataBinding(dataBinding => dataBinding.Ajax().Select("ShowCourierCommissionList", "Logistics", Model))
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .EnableCustomBinding(true))
            </td>
        </tr>
    </table>
}


<script type="text/javascript">
    var reportAggregates = "";

    $(document).ready(function () {

        //search button
        $('#search-courier').click(function () {
            //search
            var grid = $('#courier-plan-grid-admin').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
    });

    function onDataBinding(e) {
        var searchModel = {
            StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
            EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
            CourierId: $('#CourierId').val(),
        };
        e.data = searchModel;
    }

    function onDataBound(e) {
        if (reportAggregates != "") {
            updateAggregates(reportAggregates);
        }
    }

    function onComplete(e) {
        var result = e.response;
        if (result) {
            reportAggregates = e.response["aggregates"];
        }
    }

    function updateAggregates(aggs) {
        for (var key in aggs) {
            $('#aggregator-fail-delivery-block').text(aggs['aggregatorfaildelivery']);
            $('#aggregator-fail-pickup-block').text(aggs['aggregatorfailpickup']);
            $('#aggregator-success-delivery-block').text(aggs['aggregatorsuccessdelivery']);
            $('#aggregator-success-pickup-block').text(aggs['aggregatorsuccesspickup']);
        }
    }
</script>

